//
//  ZCScroView.m
//  封装了个ZCScrollView
//
//  Created by xalo on 16/6/22.
//  Copyright © 2016年 蓝鸥科技有限公司西安分公司. All rights reserved.
//

#import "ZCScroView.h"

#define kWidthFrame self.frame.size.width
#define kHeightFrame self.frame.size.height

@interface ZCScroView ()<UIScrollViewDelegate>
//scroll
@property (nonatomic,strong)UIScrollView *scroll;
//左边视图相框
@property (nonatomic,strong)UIImageView *leftImageView;
//右边视图相框
@property (nonatomic,strong)UIImageView *rightImageView;
//放传进来照片参数的数组
@property (nonatomic,strong)NSArray *array;
//记录数据，下标
@property (nonatomic,assign)NSInteger index;
//小圆点
@property (nonatomic,strong)UIPageControl *pageControl;
//计时器
@property (nonatomic,strong)NSTimer *timer;

@end


@implementation ZCScroView

//小圆点
-(UIPageControl *)pageControl{
    if (!_pageControl) {
        _pageControl = [[UIPageControl alloc]initWithFrame:CGRectMake(30, kHeightFrame-50, 200, 30)];
        _pageControl.numberOfPages = self.array.count;
    }
    return _pageControl;
}
//创建动图
- (UIScrollView *)scroll{
    if (!_scroll) {
        _scroll = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, kWidthFrame, kHeightFrame)];
        _scroll.contentSize = CGSizeMake(kWidthFrame*2, 0);
        _scroll.delegate = self;
        _scroll.showsHorizontalScrollIndicator = NO;
        _scroll.pagingEnabled = YES;
        _scroll.bounces = NO;
    }
    
    return _scroll;
}
//左边视图
- (UIImageView *)leftImageView{
    if (!_leftImageView) {
        _leftImageView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, kWidthFrame, kHeightFrame)];
//        _leftImageView.backgroundColor = [UIColor purpleColor];
        [_leftImageView sd_setImageWithURL:[NSURL URLWithString:self.array[0]]];
    }
    return _leftImageView;
}
//右边视图
- (UIImageView *)rightImageView{
    if (!_rightImageView) {
        _rightImageView = [[UIImageView alloc]initWithFrame:CGRectMake(kWidthFrame, 0, kWidthFrame, kHeightFrame)];
//        _rightImageView.backgroundColor = [UIColor greenColor];
        [_rightImageView sd_setImageWithURL:[NSURL URLWithString:self.array[self.index]]];
    }
    return _rightImageView;
}

//计时器
-(NSTimer *)timer{
    
    _timer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(timerAction) userInfo:nil repeats:YES];
    return _timer;
}

- (instancetype)initWithFrame:(CGRect)frame array:(NSArray *)array{
    self = [super initWithFrame:frame];
    if (self) {
        self.index = 1;
//        传入的array是创建的数组
        self.array = array;
        
        [self creatUI];
    }
    return self;
}

//代理协议方法
- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
    if (self.scroll.contentOffset.x == kWidthFrame) {
        self.scroll.contentOffset = CGPointZero;
        
        self.pageControl.currentPage = self.index;
        self.index++;
        if (self.index == self.array.count) {
            self.index = 0;
        }
        self.leftImageView.image = self.rightImageView.image;
        
        [self.rightImageView sd_setImageWithURL:[NSURL URLWithString:self.array[self.index]]];
        
    }

}



//计时器的方法
- (void)timerAction{
    
    [self.scroll setContentOffset:CGPointMake(kWidthFrame, 0) animated:YES];
}
//配置显示的东西
- (void)creatUI{
    
    [self addSubview:self.scroll];
    [self.scroll addSubview:self.leftImageView];
    [self.scroll addSubview:self.rightImageView];
    [self addSubview:self.pageControl];
    
    
//    self.rightImageView.image = self.array[self.index];
    
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        [self.timer fire];
    });

    
    
}





@end
